package com.redpacketcover.rc.goods;

import android.widget.Toast;

import androidx.lifecycle.MutableLiveData;

import com.redpacketcover.rc.base.BaseApplication;
import com.redpacketcover.rc.base.BaseResponseBean;
import com.redpacketcover.rc.base.BasicPresenter;
import com.redpacketcover.rc.base.ModelObserver;
import com.redpacketcover.rc.base.ViewModelFactory;
import com.redpacketcover.rc.bean.GoodsBean;
import com.redpacketcover.rc.bean.GoodsRecord;

import java.util.HashMap;
import java.util.List;

public class GoodsPresenter extends BasicPresenter<IMatchView, MatchMode> {
    @Override
    public MatchMode getModeInstance() {
        return ViewModelFactory.getInstance(MatchMode.class, mView);
    }

    /*TODO接口api管理*/
    /**
     * 获取商品列表接口
     */
    public void getGoodsList(Integer page) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", 20);
        MutableLiveData<BaseResponseBean<List<GoodsRecord>>> liveData = mMode.getGoodsList(params);
        if (!liveData.hasObservers()) {
            liveData.observe(mView, new ModelObserver<List<GoodsRecord>>() {
                @Override
                public void onSuccess(List<GoodsRecord> data) {
                    mView.showGoodsListResult(data);
                }

                @Override
                public void onFailed(int code, String errMsg) {
                    Toast.makeText(BaseApplication.getInstance(), errMsg, Toast.LENGTH_LONG).show();
                }

            });
        }
    }

    /**
     * 获取商品详情接口
     */
    public void getGoodsDetail(Integer id) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("id", id);
        MutableLiveData<BaseResponseBean<GoodsRecord>> liveData = mMode.getGoodsDetail(params);
        if (!liveData.hasObservers()) {
            liveData.observe(mView, new ModelObserver<GoodsRecord>() {
                @Override
                public void onSuccess(GoodsRecord data) {
                    mView.showGoodsDetailResult(data);
                }

                @Override
                public void onFailed(int code, String errMsg) {
                    Toast.makeText(BaseApplication.getInstance(), errMsg, Toast.LENGTH_LONG).show();
                }

            });
        }
    }

    /**
     * 获取购物车列表
     */
    public void getCartsList() {
        HashMap<String, Object> params = new HashMap<>();
        MutableLiveData<BaseResponseBean<GoodsRecord>> liveData = mMode.getCartsList(params);
        if (!liveData.hasObservers()) {
            liveData.observe(mView, new ModelObserver<GoodsRecord>() {
                @Override
                public void onSuccess(GoodsRecord data) {
                    mView.showcartsListResult(data);
                }

                @Override
                public void onFailed(int code, String errMsg) {
                    Toast.makeText(BaseApplication.getInstance(), errMsg, Toast.LENGTH_LONG).show();
                }

            });
        }
    }

}
